package com.retail.lottery.colalottery.controller;

import com.alibaba.fastjson.JSONObject;
import com.retail.lottery.colalottery.annotation.DecryptResponse;
import com.retail.lottery.colalottery.annotation.EncryptResponse;
import com.retail.lottery.colalottery.config.AzureKeyVaultConfig;
import com.retail.lottery.colalottery.service.impl.CheckDateService;
import com.retail.lottery.colalottery.utils.RedisUtils;
import com.retail.lottery.colalottery.vo.EncryptCommonRequestVo;
import com.retail.lottery.colalottery.vo.ResultVO;
import com.retail.lottery.colalottery.vo.request.CheckDateRequest;
import com.retail.lottery.colalottery.vo.request.QuestionAnswerRequest;
import com.retail.lottery.colalottery.vo.request.RedisCacheKeyRequest;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/testC")
public class TestController {

    @Resource
    private RedisTemplate redisTemplate;

    @Autowired
    private AzureKeyVaultConfig azureKeyVaultConfig;
//    @Autowired
//    private SecretClient secretClient;
    @Resource
    private CheckDateService checkDateService;

    @EncryptResponse
    @DecryptResponse
    @RequestMapping("/test")
    public ResultVO<Map<String,String>> test(@RequestBody JSONObject requestVo)
    {
        Map<String,String> map = new HashMap<>();
        map.put("name","123");
        return ResultVO.succ(map);
    }

//    @GetMapping("/secret")
//    public String getSecret() {
//        return "Secret from Key Vault: " + secretClient.getSecret("secret-name").getValue();
//    }

//    private TelemetryClient telemetryClient;
//
//    @GetMapping("/appInsights")
//    public String sendMessageToAi() {
//
//        telemetryClient = new TelemetryClient();
//        telemetryClient.trackEvent("Hello Application Insights events...!");
//        telemetryClient.trackTrace("this message from spring cloud application ... local test.");
//
//        return "............................................................................................................................";
//    }

    @RequestMapping(value = "/checkQuery" , consumes = "text/plain")
    public ResultVO<List<String>> checkQuery(@RequestBody String request)
    {
        return ResultVO.succ(checkDateService.check(request));
    }

    @RequestMapping(value = "/checkExecute" , consumes = "text/plain")
    public ResultVO<Object> checkExecute(@RequestBody String request)
    {
        return ResultVO.succ(checkDateService.execute(request));
    }

    @PostMapping("/clearCache")
    public ResultVO<?> clearCache(@RequestBody RedisCacheKeyRequest cacheKeyRequest) {
        return ResultVO.succ(redisTemplate.delete(cacheKeyRequest.getRedisKey()));
    }

    @PostMapping("/addCache")
    public ResultVO<?> addCache(@RequestBody RedisCacheKeyRequest cacheKeyRequest) {
        RedisUtils.saveString(cacheKeyRequest.getRedisKey(), cacheKeyRequest.getValue(), cacheKeyRequest.getExpiresIn(), TimeUnit.SECONDS);
        return ResultVO.succ();
    }
}
